package com.prtek.reminder.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.prtek.reminder.R;
import com.prtek.reminder.adapter.ContactSelectAdapter;
import com.prtek.reminder.bean.User;
import com.prtek.reminder.listener.OnConfirmClickListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jarly on 2018/9/15.
 */

public class SelectUserDialog extends Dialog implements AdapterView.OnItemClickListener {
  @BindView(R.id.contact_gv) GridView mContactGv;
  @BindView(R.id.confirm_contact_btn) Button mConfirmContactBtn;
  @BindView(R.id.cancel_contact_btn) Button mCancelContactBtn;

  private OnConfirmClickListener mListener;
  private List<User> mContactSelectedList = new ArrayList<>();
  private ContactSelectAdapter mContactAdapter;

  public SelectUserDialog(@NonNull Context context) {
    super(context, android.R.style.Theme_Holo_NoActionBar_Fullscreen);
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_window);
    ButterKnife.bind(this);
    mContactAdapter = new ContactSelectAdapter(getContext());
    mContactGv.setAdapter(mContactAdapter);
    mContactGv.setSelector(new ColorDrawable(Color.TRANSPARENT));
    mContactGv.setOnItemClickListener(this);
  }

  public void setData(List<User> userList) {
    mContactAdapter.setDataList(userList);
  }

  @OnClick({ R.id.confirm_contact_btn, R.id.cancel_contact_btn })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.confirm_contact_btn:
        onConfirm();
        break;
      case R.id.cancel_contact_btn:
        onCancel();
        break;
    }
    dismiss();
  }

  public void onCancel() {
    mContactSelectedList.clear();
  }

  public void onConfirm() {
    mListener.onConfirmClick(mContactSelectedList);
  }

  @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    final User user = (User) mContactAdapter.getItem(i);
    refreshenContact(user);
  }

  /**
   * 添加已选择的用户,并刷新界面
   *
   * @param user
   * @return
   */
  public void refreshenContact(User user) {

    final boolean selection = !user.isSelected();

    if (selection) {
      mContactSelectedList.add(user);
    } else {
      mContactSelectedList.remove(user);
    }
    user.setSelected(selection);
    mContactAdapter.notifyDataSetChanged();

  }

  public void setOnConfirmClickListener(OnConfirmClickListener listener) {
    mListener = listener;
  }

  public void addSelectUser(String userName) {

    final List<User> dataList = mContactAdapter.getDataList();
    for (int i = 0; i<dataList.size();i++) {
      final User user = dataList.get(i);
      final String name = user.getName();
      if (name.equals(userName)) {
        refreshenContact(user);
      }
    }
  }

  @Override public void dismiss() {
    super.dismiss();
    mContactSelectedList.clear();
  }

  public void setSelectUsers(User user) {
    mContactSelectedList.add(user);
  }
}
